import { Color, Mesh, Texture } from 'three'
import { GlobalContainer } from '../common/GlobalContainer'
import { Point2D } from './dto/Point2D'
import { HouseApplication } from './HouseApplication'

import { OBJLoader } from 'three/examples/jsm/Addons.js'
import { MTLLoader } from 'three/examples/jsm/Addons.js'

export class TestApplication extends HouseApplication {
  constructor() {
    super()
  }

  public testLogPage() {
    console.log('page:', GlobalContainer.doServiceContainer.designPageService.getPage())
    console.log('sceneController:', GlobalContainer.threeSceneController)
    console.log('getRendererInfo:', GlobalContainer.threeRender3DController.getRendererInfo())
  }

  public testCreateZone() {
    this.houseDesignAppService.createZone([
      new Point2D(-3000, -3000),
      new Point2D(-3000, 3000),
      new Point2D(3000, 3000),
      new Point2D(3000, -3000),
    ])
  }

  /**
   * 测试加载zip文件资源
   */
  public testLoadZip() {
    const url = '/model/old_television.zip'
    GlobalContainer.zipLoader.loadZipUrl(url).then(() => {
      console.log('loadZipUrl success')
    })
  }

  /**
   * 测试直接加载3ds
   */
  public async testLoad3DS() {
    GlobalContainer.tdsLoader
      .loadTDSModel('/model/chair/BLA STATION - STING_O30 Serie_Chairs N080123.3ds')
      .then((obj) => {
        console.log('loadTDSModel success')
        console.log(obj)
      })
  }

  /**
   * 测试加载3ds zip文件资源
   */
  public async testLoad3DSZip() {
    // https://www.youtube.com/@Archibase.Planet/shorts 资源

    // 不行，max只能用于3ds编辑器，需要导出为.3ds
    // 3ds可以自身内含贴图，也可以通过外部匹配导出的贴图

    let url = '/model/chair.zip'
    const tableUrl = '/model/bedside_table.zip'
    const cabinetUrl = '/model/cabinet_柜子_3ds.zip'

    // url = chairUrl
    // url = '/model/OuterLock.zip'
    url = tableUrl
    url = cabinetUrl

    const zipLoadResult = await GlobalContainer.zipLoader.loadZipUrl(url)

    console.log(zipLoadResult)

    for (const dirBlobData of zipLoadResult) {
      const filePaths = Array.from(dirBlobData.blobPathUrlMap.keys())
      const modelPath = filePaths.find((path) => path.endsWith('.3ds') || path.endsWith('.3DS'))

      if (!modelPath) {
        throw new Error('no found 3ds model file')
      }

      const modelGroup = await GlobalContainer.tdsLoader.loadModel(modelPath, dirBlobData)
      if (modelGroup) {
        console.log('loadTDSZipModel success:', modelGroup)
        GlobalContainer.threeSceneController.scene.add(modelGroup)
      }

      modelGroup.traverse((child) => {
        console.log('child:', child)
        if (child instanceof Mesh) {
          // if (child.material instanceof MeshPhongMaterial) {
          //   const oldMaterial = child.material
          //   console.log(oldMaterial)
          //   child.material = new MeshBasicMaterial({
          //     side: 2,
          //   })
          //   if (oldMaterial.map) {
          //     // child.material.map = oldMaterial.map
          //   }
          //   if (oldMaterial.color) {
          //     child.material.color = oldMaterial.color
          //   }
          // } else {
          //   console.log('no material:', child)
          // }
        }
      })
    }
  }

  public async testLoadGLTFModel() {
    let url = ''

    url = '/gltf model/myCemera.glb'
    url = '/gltf model/small chair/small chair.glb'
    url = '/gltf model/office chair/office chair.gltf'
    const group = await GlobalContainer.threeModelResourceManager.loadModel(url)
    if (group) {
      GlobalContainer.threeSceneController.scene.add(group)
    }
  }

  public async testLoadObjModel() {
    const mtlUrl = '/model/bedside_table/Beautiful Dreams Nightstand N271223.mtl'
    const objUrl = '/model/bedside_table/Beautiful Dreams Nightstand N271223.obj'

    // 先加载mtl，用于为obj加载材质
    const mtlLoader = new MTLLoader()
    const mtl = await mtlLoader.loadAsync(mtlUrl)
    console.log('mtl:', mtl)

    const objLoader = new OBJLoader()
    objLoader.setMaterials(mtl) // 设置mtl

    const obj = await objLoader.loadAsync(objUrl)
    console.log('loadObjModel success:', obj)
    GlobalContainer.threeSceneController.scene.add(obj)

    // 加载obj材质
    // https://blog.csdn.net/yy729851376/article/details/124421674
    // 我是一个threejs开发者，在网上找模型资源时，找到了一个柜子模型的资源，发现下载的zip压缩包中同时带有: .3ds .gsm  .mtl .obj的文件，这些文件有啥用
  }

  public async initGui() {
    if (GlobalContainer.threeSceneController.ambientLight) {
      this.guiApplication.debugLight(GlobalContainer.threeSceneController.ambientLight)
    }
  }
}

export interface IMaterial {
  map: Texture | Texture[] | null
  color: Color | null
}
